package flc.ast.activity;

import android.view.View;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.youth.banner.listener.OnPageChangeListener;

import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.PicDetailAdapter;
import flc.ast.databinding.ActivityPicDetailBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.IntentUtil;
import stark.common.basic.utils.RxUtil;

public class PicDetailActivity extends BaseAc<ActivityPicDetailBinding> {

    public static List<String> listPath;
    public static int selPosition;
    private PicDetailAdapter picDetailAdapter;

    @Override
    protected int onCreate() {
        return R.layout.activity_pic_detail;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivPicDetailBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivShare.setOnClickListener(this);
        mDataBinding.ivDelete.setOnClickListener(this);

        mDataBinding.banner.isAutoLoop(false);
        picDetailAdapter = new PicDetailAdapter(listPath);
        mDataBinding.banner.setAdapter(picDetailAdapter, false);//false不无限循环
        mDataBinding.banner.setCurrentItem(selPosition, false);
        mDataBinding.banner.addOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                selPosition = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivShare://分享
                IntentUtil.shareImage(mContext, getString(R.string.share), listPath.get(selPosition));
                break;
            case R.id.ivDelete://删除
                delete();
                break;
        }
    }

    @Override
    protected void initData() {

    }

    //删除
    private void delete() {
        showDialog(getString(R.string.delete_ing));
        RxUtil.create(new RxUtil.Callback<String>() {

            @Override
            public void doBackground(ObservableEmitter<String> emitter) {
                FileUtils.delete(listPath.get(selPosition));
                emitter.onNext("");
            }

            @Override
            public void accept(String path) {
                dismissDialog();
                ToastUtils.showShort(getString(R.string.delete_suc));
                finish();
            }
        });
    }

}